<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Examine;
use App\Admin\Repositories\UserRelease;
use App\library\services\UserReleaseService;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;

class UserReleaseController extends AdminController
{
    /**
     * Author:Zhongyuan Lu
     * 发布信息列表
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new UserRelease(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('housing_eatate');    //小区名称
            $grid->column('house_type');
            $grid->column('config');    //配套设置
            $grid->column('area');      //面积
            $grid->column('price');     //总价
            $grid->column('budget');    //预算
            $grid->column('effective_time');
            $grid->column('type')->display(function ($type) {
                $options = config('setting.release_type');
                return $options[$type];
            });
            $grid->column('status');
            //状态（0:待审核 1：审核成功 -1 未通过审核）
            $grid->column('status')->display(function ($status) {
                $options = config('setting.examine_status');
                return $options[$status];
            });
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->disableEdit();
                $actions->disableQuickEdit();
//                $id = $actions->getKey();
//                $url = URL::current();
                // append一个操作
                $actions->append(new Examine('通过'));
                $actions->append(new Examine('驳回'));
//                $actions->append("<a href={$url}>通过</a>");
//                $actions->append("<a href={$url}>驳回</a>");
            });

//            $grid->disableCreateButton();
//            $grid->disableEditButton();
//            $grid->disableQuickEditButton();
        });
    }

    /**
     * Author:Zhongyuan Lu
     * 审核
     * @param Request $request
     * @return array
     */
    public function examine(Request $request)
    {
        $id = $request->input('id');
        $status = $request->input('status');
        return UserReleaseService::examine($id,$status);
    }

    /**
     * Author:Zhongyuan Lu
     * 发布详情查看
     * @param $id
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UserRelease(), function (Show $show) {
            $show->field('id');
            $show->field('uid');
            $show->field('title');
            $show->field('desc');
            $show->field('username');
            $show->field('phone');
            $show->field('housing_eatate');
            $show->field('property');
            $show->field('province');
            $show->field('city');
            $show->field('region');
            $show->field('latitude');
            $show->field('longitude');
            $show->field('house_type');
            $show->field('forward');
            $show->field('fit_up');
            $show->field('config');
            $show->field('area');
            $show->field('price');
            $show->field('budget');
            $show->field('tags');
            $show->field('effective_time');
            $show->field('type');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Author:Zhongyuan Lu
     * 发布信息修改
     * @return Form
     */
    protected function form()
    {
        return Form::make(new UserRelease(), function (Form $form) {
            $form->display('id');
            $form->text('uid');
            $form->text('title');
            $form->text('desc');
            $form->text('username');
            $form->text('phone');
            $form->text('housing_eatate');
            $form->text('property');
            $form->text('province');
            $form->text('city');
            $form->text('region');
            $form->text('latitude');
            $form->text('longitude');
            $form->text('house_type');
            $form->text('forward');
            $form->text('fit_up');
            $form->text('config');
            $form->text('area');
            $form->text('price');
            $form->text('budget');
            $form->text('tags');
            $form->text('effective_time');
            $form->text('type');
            $form->text('status');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
